package torres.controller;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;

import torres.RunGame;
import torres.ability.Ability;
import torres.ability.AbilityManager;
import torres.entity.Entity;
import torres.entity.Merchant;
import torres.entity.NPC;
import torres.entity.Skill;
import torres.item.EquippableItem;
import torres.item.EquippedInventory;
import torres.item.Inventory;
import torres.item.TakeableItem;
import torres.item.Weapon;
import torres.map.Location;
import torres.map.World;
import torres.view.AbilityMenuView;
import torres.view.ActionMenuView;
import torres.view.InventoryView;
import torres.view.MainView;
import torres.view.PurchaseMenuView;
import torres.view.ShopMenuView;
import torres.view.SkillMenuView;

/**
 * @author Torres
 * A utility class that allows commands to be issued to Entities.
 * 
 */
public class Command {
	
	private static boolean skillMenuShown = false;
	private static boolean dialogueMenuShown = false;
	private static boolean shopMenuShown = false;
	private static boolean purchaseMenuShown = false;
	private static boolean actionInventoryMenuShown = false;
	
	/**
	 * The method allows the Entity to move the direction passed in.
	 * @param d the direction the entity will move
	 * @param e the entity that wants to move
	 */
	public static void moveEntity(Direction d, Entity e) {
		e.move(d);
		if(World.getInstance().getPlayer().getActionMenu()) {
			pause();
		}
	}
	
	/**
	 * The method shows the Entity's Inventory
	 */
	public static void showInventory() {
		if(RunGame.gameLoop.getPause() == true && RunGame.gameLoop.getInventory() == false) {
			
		}
		else if(RunGame.gameLoop.getPause() == false && RunGame.gameLoop.getInventory() == false) {
			pause();
			RunGame.gameLoop.toggleInventory();
			if(RunGame.gameLoop.getInventory() == true) {
				MainView.getInstance().getInventoryView().update(MainView.getInstance().getGraphics());
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getPause() == true && RunGame.gameLoop.getInventory() == true) {
			RunGame.gameLoop.toggleInventory();
			pause();
		}
	}
	
	public static void showActionInventory() {
		if(RunGame.gameLoop.getActionInventoryMenu() == true && actionInventoryMenuShown == false) {
			
		}
		else if(RunGame.gameLoop.getActionInventoryMenu() == false && actionInventoryMenuShown == false) {
			actionInventoryMenu();
			actionInventoryMenuShown = !actionInventoryMenuShown;
			if(RunGame.gameLoop.getInventory() == true) {
				MainView.getInstance().getInventoryView().update(MainView.getInstance().getGraphics());
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getActionInventoryMenu() == true && actionInventoryMenuShown == true) {
			actionInventoryMenuShown = !actionInventoryMenuShown;
			//RunGame.gameLoop.toggleInventory();
			actionInventoryMenu();
		}
	}
	
	/**
	 * When the Inventory menu is open, used to interact with items in Inventory.
	 * @param e MouseEvent for interaction
	 */
	public static void interactWithItem(MouseEvent e) {
		InventoryView view = MainView.getInstance().getInventoryView();
		HashMap<Integer,Dimension> table = view.getInventoryTable();
		HashMap<Integer,Dimension> table2 = view.getEquippedTable();
		Entity player = World.getInstance().getPlayer();
		Inventory inventory = player.getInventory();
		EquippedInventory equippedInventory = player.getEquippedInventory();
		List<TakeableItem> itemList = inventory.getItemList();
		List<EquippableItem> equippedItemList = equippedInventory.getItemList();
		
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					TakeableItem ti = itemList.get(i-1);
					ti.use(player);
				}
			}
		}
		else if(e.getButton() == MouseEvent.BUTTON3) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					Location location = new Location(player.getLocation().getX()+1,player.getLocation().getY());
					World.getInstance().addItem(itemList.remove(i-1),location);

				}
			}
			for (int i=1; i <= table2.size(); i++) {
				Dimension dim2 = table2.get(i);
				if(xPos >= dim2.getWidth() && xPos <= (dim2.getWidth() + 80) &&
						yPos >= dim2.getHeight() && yPos <= (dim2.getHeight() + 80)) {
					EquippableItem ei = equippedItemList.get(i-1);
					System.out.println("Command: " + (ei instanceof Weapon));
					ei.removeFromEquipInventory(equippedInventory);
					//EquippableItem temp = equippedInventory.removeItem(equippedItemList.get(i-1));
					itemList.add(ei);
				}
			}
		}
		
		if (xPos >= (view.getScreenWidth()/2 + view.getX()/2 - 50) && 
				xPos <= (view.getScreenWidth()/2 + view.getX()/2)  &&
				yPos >= (view.getScreenHeight()/2 - view.getY()/2) &&
				yPos <= (view.getScreenHeight()/2 - view.getY()/2 + 50)) {
			showInventory();
		}

		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void interactWithActionItem(MouseEvent e) {
		InventoryView view = MainView.getInstance().getInventoryView();
		HashMap<Integer,Dimension> table = view.getInventoryTable();
		HashMap<Integer,Dimension> table2 = view.getEquippedTable();
		Entity player = World.getInstance().getPlayer();
		Inventory inventory = player.getInventory();
		EquippedInventory equippedInventory = player.getEquippedInventory();
		List<TakeableItem> itemList = inventory.getItemList();
		List<EquippableItem> equippedItemList = equippedInventory.getItemList();
		
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					TakeableItem ti = itemList.get(i-1);
					ti.use(player, World.getInstance().getPlayer().getInteractedWith());
					System.out.println("Using item on: "+World.getInstance().getPlayer().getInteractedWith());
				}
			}
		}
		else if(e.getButton() == MouseEvent.BUTTON3) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					Location location = new Location(player.getLocation().getX()+1,player.getLocation().getY());
					World.getInstance().addItem(itemList.remove(i-1),location);

				}
			}
			for (int i=1; i <= table2.size(); i++) {
				Dimension dim2 = table2.get(i);
				if(xPos >= dim2.getWidth() && xPos <= (dim2.getWidth() + 80) &&
						yPos >= dim2.getHeight() && yPos <= (dim2.getHeight() + 80)) {
					EquippableItem ei = equippedItemList.get(i-1);
					System.out.println("Command: " + (ei instanceof Weapon));
					ei.removeFromEquipInventory(equippedInventory);
					//EquippableItem temp = equippedInventory.removeItem(equippedItemList.get(i-1));
					itemList.add(ei);
				}
			}
		}
		
		if (xPos >= (view.getScreenWidth()/2 + view.getX()/2 - 50) && 
				xPos <= (view.getScreenWidth()/2 + view.getX()/2)  &&
				yPos >= (view.getScreenHeight()/2 - view.getY()/2) &&
				yPos <= (view.getScreenHeight()/2 - view.getY()/2 + 50)) {
			showActionInventory();
		}

		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void interactWithPurchaseMenu(MouseEvent e) {
		PurchaseMenuView view = MainView.getInstance().getPurchaseMenuView();
		
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			if(xPos >= view.getX() && xPos <= (view.getX() + view.getWidth()) &&
				yPos >= view.getY() && yPos <= (view.getY() + view.getHeight())) {
				if(view.getText().equals("BUY")) {
					((Merchant)World.getInstance().getPlayer().getInteractedWith()).sellItem(view.getItem(), World.getInstance().getPlayer());
				}
				else {
					((Merchant)World.getInstance().getPlayer().getInteractedWith()).buyItem(view.getItem(), World.getInstance().getPlayer());
				}
				purchaseMenuShown = !purchaseMenuShown;
				RunGame.gameLoop.togglePurchaseMenu();
			}
		}
		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void interactWithShop(MouseEvent e) {
		ShopMenuView view = MainView.getInstance().getShopMenuView();
		HashMap<Integer,Dimension> table = view.getPlayerInventoryTable();
		HashMap<Integer,Dimension> table2 = view.getMerchantInventoryTable();
		Entity player = World.getInstance().getPlayer();
		Merchant merchant = (Merchant)World.getInstance().getPlayer().getInteractedWith();
		Inventory playerInventory = player.getInventory();
		List<TakeableItem> itemList = playerInventory.getItemList();
		List<TakeableItem> merchantInventory = (List<TakeableItem>) merchant.getShopList();
		
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					TakeableItem ti = itemList.get(i-1);
					showPurchaseMenu(ti, "SELL");
				}
			}
			for (int i=1; i <= table2.size(); i++ ) {
				Dimension dim = table2.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + 80) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + 80)) {
					TakeableItem ti = merchantInventory.get(i-1);
					showPurchaseMenu(ti, "BUY");
				}
			}
		}
		
		if (xPos >= (view.getScreenWidth()/2 + view.getX()/2 - 50) && 
				xPos <= (view.getScreenWidth()/2 + view.getX()/2)  &&
				yPos >= (view.getScreenHeight()/2 - view.getY()/2) &&
				yPos <= (view.getScreenHeight()/2 - view.getY()/2 + 50)) {
			
			showShopMenu(merchant);
		}
		
		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	
	/**
	 * @param e
	 */
	public static void interactWithSkillPoint(MouseEvent e, HashMap<Skill, Point> skillHash, int radius) {
		Skill[] skills = Skill.values();
		//System.out.println("Command.interactWithAbilityView(): rect.contains(point): "+ rect.contains(e.getPoint()) + " Rect: " + rect.getLocation());
		for (Skill skill: skills)
				if(withinCircularRange(skillHash.get(skill), radius, e.getPoint())) {
					World.getInstance().getPlayer().allocateSkillPoint(skill);
					//System.out.println("Command.interactWithSkillView: You Clicked on " + skill );
					break;
				}
		int xPos = e.getX();
		int yPos = e.getY();
		AbilityMenuView view = MainView.getInstance().getAbilityMenuView();
		
		if (xPos >= (view.getScreenWidth()/2 + view.getX()/2 - 50) && 
				xPos <= (view.getScreenWidth()/2 + view.getX()/2)  &&
				yPos >= (view.getScreenHeight()/2 - view.getY()/2) &&
				yPos <= (view.getScreenHeight()/2 - view.getY()/2 + 50)) {
			showAbilityMenuView();
		}
		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void interactWithAbility(MouseEvent e, HashMap<Ability, Rectangle> hashRect) {
		 LinkedHashSet<Ability> attackList =  World.getInstance().getPlayer().getOccupation().getAttackList();
		 LinkedHashSet<Ability> abilityList = World.getInstance().getPlayer().getOccupation().getAbilityList();
		 //System.out.println("Command.InteractWithAbility called");
		//Remove attacks from the attack list
		for (Ability arr : attackList) {
			if (hashRect.get(arr).contains(e.getPoint())) {
				attackList.remove(arr);
				System.out.println("Command.interactWithAbility(): Removed Ability: " + arr.getName());
				return;
			}
		}
		for (Ability arr : abilityList) {
			if (hashRect.get(arr).contains(e.getPoint())) {
				//System.out.println("This Happend(1)");
				if (attackList.size() < 3) {
					attackList.add(arr);
					System.out.println("Command.interactWithAbility(): Added Ability: " + arr.getName());
				}
				return;
			}
		}
		
	
	}
	
	/**Checks logic for SkillPointView Menu.  If the game is paused
	 * the view opens up if its toggled off. Otherwise, the game unpauses
	 * and toggles the menu off.  This is called through the torres.controller.Controller
	 * class.
	 */
	public static void showAbilityMenuView() {
		if (!RunGame.gameLoop.getPause() && !RunGame.gameLoop.getAbilityMenu()) {
			pause();
			RunGame.gameLoop.toggleAbilityMenu();
			if(RunGame.gameLoop.getAbilityMenu()) {
				MainView.getInstance().getAbilityMenuView().update(MainView.getInstance().getGraphics());
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getPause() && RunGame.gameLoop.getAbilityMenu()) {
			RunGame.gameLoop.toggleAbilityMenu();
			pause();
		}
		else {
			//Do nothing
			System.err.println("Command.showSkillPointView: Error, this option should not occurr.");
			System.out.println("\tPause: " + RunGame.gameLoop.getPause() + "Inventory: " + RunGame.gameLoop.getAbilityMenu());
		}
		
	}
	/**
	 * The method pauses the game and prevents further actions 
	 */
	public static void showDialogueMenu(NPC e, double x, double y) {
		if(RunGame.gameLoop.getActionMenu() == true && dialogueMenuShown == false) {
			
		}
		else if(RunGame.gameLoop.getActionMenu() == false && dialogueMenuShown == false) {
			actionMenu();
			dialogueMenuShown = !dialogueMenuShown;
			if(RunGame.gameLoop.getActionMenu() == true) {
				//System.out.println("Command.ShowInventory");
				MainView.getInstance().getDialogueMenuView().update(MainView.getInstance().getGraphics(), e, x, y);
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getActionMenu() == true && dialogueMenuShown == true) {
			dialogueMenuShown = !dialogueMenuShown;
			actionMenu();
		}
	}
	
	public static void showSkillMenu(double x, double y, int width, int height) {
		if(RunGame.gameLoop.getSkillMenu() == true && skillMenuShown == false) {
			
		}
		else if(RunGame.gameLoop.getSkillMenu() == false && skillMenuShown == false) {
			skillMenu();
			skillMenuShown = !skillMenuShown;
			if(RunGame.gameLoop.getSkillMenu() == true) {
				//System.out.println("Command.ShowInventory");
				MainView.getInstance().getSkillMenuView().update(MainView.getInstance().getGraphics(), x, y, width, height);
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getSkillMenu() == true && skillMenuShown == true) {
			skillMenuShown = !skillMenuShown;
			skillMenu();
		}
	}
	
	public static void showPurchaseMenu(TakeableItem t, String text) {
		if(RunGame.gameLoop.getPurchaseMenu() == true && purchaseMenuShown == false) {
			
		}
		else if(RunGame.gameLoop.getPurchaseMenu() == false && purchaseMenuShown == false) {
			purchaseMenu();
			purchaseMenuShown = !purchaseMenuShown;
			if(RunGame.gameLoop.getPurchaseMenu() == true) {
				//System.out.println("Command.ShowInventory");
				MainView.getInstance().getPurchaseMenuView().update(MainView.getInstance().getGraphics(), t, text);
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getPurchaseMenu() == true && purchaseMenuShown == true) {
			purchaseMenuShown = !purchaseMenuShown;
			purchaseMenu();
		}
	}

	public static void showShopMenu(Merchant m) {
		if(RunGame.gameLoop.getShopMenu() == true && shopMenuShown == false) {
			
		}
		else if(RunGame.gameLoop.getShopMenu() == false && shopMenuShown == false) {
			shopMenu();
			shopMenuShown = !shopMenuShown;
			if(RunGame.gameLoop.getShopMenu() == true) {
				//System.out.println("Command.ShowInventory");
				MainView.getInstance().getShopMenuView().update(MainView.getInstance().getGraphics(), m);
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getShopMenu() == true && shopMenuShown == true) {
			shopMenuShown = !shopMenuShown;
			shopMenu();
		}
	}
	
	public static void showActionMenu() {
		System.out.println(World.getInstance().getPlayer().getActionMenu());
		if(RunGame.gameLoop.getPause() == false && World.getInstance().getPlayer().getActionMenu() == true){
			pause();
			World.getInstance().getPlayer().showActionMenu();
			if(World.getInstance().getPlayer().getActionMenu() == true) {
				MainView.getInstance().getActionMenuView().update(MainView.getInstance().getGraphics());
				BufferStrategy strategy = MainView.getInstance().getBufferStrategy();
				strategy.show();
				Toolkit.getDefaultToolkit().sync();
			}
		}
		else if(RunGame.gameLoop.getPause() == true && World.getInstance().getPlayer().getActionMenu() == true) {
			World.getInstance().getPlayer().showActionMenu();
			pause();
		}
	}
	
	public static void interactWithActionMenu(MouseEvent e) {
		ActionMenuView view = MainView.getInstance().getActionMenuView();
		HashMap<Integer,Dimension> table = view.getActionMenuTable();
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			for (int i=0; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(dim==null) continue;
				
				if(i==0) {
					if(xPos <= dim.getWidth() && xPos >= (dim.getWidth() - view.getWidth()/3)
						&& yPos <= dim.getHeight() && yPos >= (dim.getHeight() - view.getHeight()/3)) {
						showActionMenu();
						break;
					}
					else continue;
				}
				
				
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + view.getWidth()) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + view.getHeight())) {
					if(i==1) {
						showDialogueMenu((NPC)World.getInstance().getPlayer().getInteractedWith(), dim.getWidth() + view.getWidth(), dim.getHeight() + view.getHeight());
					}
					else if(i==2) {
						if(World.getInstance().getCurrentMap().getEnemies().contains(World.getInstance().getPlayer().getInteractedWith())) {
							((NPC)World.getInstance().getPlayer().getInteractedWith()).setHostile();
							World.getInstance().getPlayer().interact(World.getInstance().getPlayer().getInteractedWith());
						}
						showActionMenu();
					}
					else if(i==3) {
						if(World.getInstance().getPlayer().getOccupation().getJob().name().equals("SNEAK")) {
							showSkillMenu(dim.getWidth() + view.getWidth(), dim.getHeight() + view.getHeight(), view.getWidth(), view.getHeight());
						}
					}
					else if(i==4) {
						showActionInventory();
					}
					else if(i==5) {
						showShopMenu((Merchant)World.getInstance().getPlayer().getInteractedWith());
					}
				}
			}
		}
		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void interactWithSkillMenu(MouseEvent e) {
		SkillMenuView view = MainView.getInstance().getSkillMenuView();
		HashMap<Integer,Dimension> table = view.getSkillMenuTable();
		
		int xPos = e.getX();
		int yPos = e.getY();
		
		if(e.getButton() == MouseEvent.BUTTON1) {
			for (int i=1; i <= table.size(); i++ ) {
				Dimension dim = table.get(i);
				if(xPos >= dim.getWidth() && xPos <= (dim.getWidth() + view.getWidth()) &&
					yPos >= dim.getHeight() && yPos <= (dim.getHeight() + view.getHeight())) {
					if(i==1) {
						AbilityManager.getInstance().getNonActionAbility(501).useAbility(World.getInstance().getPlayer());
						skillMenuShown = !skillMenuShown;
						RunGame.gameLoop.toggleSkillMenu();
						showActionMenu();
					}
					else if(i==2) {
						if(!World.getInstance().getPlayer().isVisible()) {
							((NPC)World.getInstance().getPlayer().getInteractedWith()).setHostile();
							AbilityManager.getInstance().getActionAbility(100).useAbility(World.getInstance().getPlayer().getInteractedWith());
							World.getInstance().getPlayer().interact(World.getInstance().getPlayer().getInteractedWith());
							skillMenuShown = !skillMenuShown;
							RunGame.gameLoop.toggleSkillMenu();
							World.getInstance().getPlayer().setVisible();
							showActionMenu();
						}
						else {
							skillMenuShown = !skillMenuShown;
							RunGame.gameLoop.toggleSkillMenu();
							showActionMenu();
						}
					}
				}
			}
		}
		MainView.getInstance().draw();
		MainView.getInstance().draw();
		MainView.getInstance().draw();
	}
	
	public static void actionMenu() {
		if(!dialogueMenuShown) {
			RunGame.gameLoop.toggleActionMenu();
		}
	}
	
	public static void skillMenu() {
		if(!skillMenuShown) {
			RunGame.gameLoop.toggleSkillMenu();
		}
	}
	
	public static void shopMenu() {
		if(!shopMenuShown) {
			RunGame.gameLoop.toggleShopMenu();
		}
	}
	
	private static void purchaseMenu() {
		if(!purchaseMenuShown) {
			RunGame.gameLoop.togglePurchaseMenu();
		}
	}
	
	private static void actionInventoryMenu() {
		if(!actionInventoryMenuShown) {
			RunGame.gameLoop.toggleActionInventoryMenu();
		}
	}

	/**
	 * Pauses the game
	 */
	public static void pause() {
		if (RunGame.gameLoop.getInventory() == false || World.getInstance().getPlayer().getActionMenu() == false
				|| RunGame.gameLoop.getAbilityMenu() == false) {
			RunGame.gameLoop.togglePause();
		}
	}
	
	public static boolean withinCircularRange(Point loc, int radius, Point clickedLoc) {
		//System.out.println("Command.withinCircularRange: loc: " + loc.toString() + " radius: " + radius + " clickedLoc: " + clickedLoc);
		try {
			return loc.distance(clickedLoc) <= radius;	
		}
		catch (Exception e) {
			System.err.println("Command.withinCircularRange: Error: There was a Null pointer exception I will fix it later.");
			return false;
		}
	}
}